<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible.sync="visible" :before-close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getUserEasy">
          <el-option v-for="item in people" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartment, getDepartmentDetail, updateDepartment } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    // 校验部门名称
    const validateName = async(rule, value, callback) => {
      // 拿到当前节点的子部门数据
      const { depts } = await getDepartments()
      // 获取当前节点的id和所有部门数据的pid做对比，只要pid和当前节点的id相同就说明该部门是当前节点子部门
      let isRepeat = false
      if (this.formData.id) {
        // 编辑 筛选兄弟节点
        isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        // 新增 筛选子节点
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error(`该部门下已存在${value}部门`)) : callback()
    }
    // 校验部门编码
    const validateCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      // 判断是编辑还是添加
      if (this.formData.id) {
        // 编辑 筛选兄弟节点
        isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value)
      } else {
        // 新增 筛选子节点
        isRepeat = depts.some(item => item.code === value)
      }
      isRepeat ? callback(new Error('部门编码重复')) : callback()
    }
    return {
      // 表单model
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '部门名称要求1-10个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '部门编码要求1-20个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      people: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
      // if (this.formData.code) {
      //   return '编辑部门'
      // } else {
      //   return '新增部门'
      // }
    }
  },
  methods: {
    async getDepartDetail(id) {
      this.formData = await getDepartmentDetail(id)
    },
    // 关闭弹窗
    handleClose() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:visible', false)
    },
    // 提交表单新增功能
    async submit() {
      // 表单校验
      await this.$refs.deptForm.validate()
      if (this.formData.id) {
        await updateDepartment(this.formData)
        this.$message.success('编辑部门成功')// 成功提示
      } else {
        // 调用接口
        await addDepartment({ ...this.formData, pid: this.treeNode.id })
        this.$message.success('新增部门成功')// 成功提示
      }
      this.$emit('addDepts')// 通知父组件
      this.$refs.deptForm.resetFields()// 重置表单
      this.handleClose()// 关闭弹层
    },
    // 获取简单用户信息
    async getUserEasy() {
      this.people = await getEmployeeSimple()
    }
  }
}
</script>

<style>

</style>
